#include "commonpage.h"
#include "ui_commonpage.h"
#include"listitem.h"
#include<QMediaPlaylist>
CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉水平滚条；
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex &index){
        //发送一个信号给miniqqmusic
        emit playMusicIndex(this,index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonUI(const QString &text, const QString &imagePath)
{
    //设置标题和图片；
    ui->pageTittle->setText(text);
    ui->musicImageLabel->setPixmap(imagePath);
    ui->musicImageLabel->setScaledContents(true);//让图片和控件相适应；
}

void CommonPage::addListItem(const QString &name, const QString &singer, const QString &altum)
{
    ListItem*listitem = new ListItem(this);
    QListWidgetItem*item = new QListWidgetItem(ui->pageMusicList);
    item->setSizeHint(QSize(ui->pageMusicList->width(),45));//设置item的初识状态；
    ui->pageMusicList->setItemWidget(item,listitem);
    (void)name;
    (void)singer;
    (void)altum;
}

void CommonPage::setPageType(PageType pageType)
{
    this->_pageType = pageType;
    //设置好每一个commonpage是哪一种page;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    _listId.clear();//清除之前的残留；
    for(auto music:musicList)
    {
        switch(_pageType)
        {
            case LIKE_PAGE:
            if(music.getIsLike())
            {
                _listId.push_back(music.getId());
            }
            break;
        case LOCK_PAGE:
            _listId.push_back(music.getId());
            break;
        case HISTORY_PAGE:
            if(music.getIsHistory())
            {
                _listId.push_back(music.getId());
            }
            break;
        default:break;

        }
    }
}

void CommonPage::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();
    addMusicToMusicPage(musicList);//更新commonpage中的ID；
    for(auto&musicId:_listId)
    {
        auto it = musicList.findId(musicId);
        if(it != musicList.end())
        {
            ListItem*item = new ListItem(this);
            item->setMusicName(it->getMusicName());
            item->setMusicSinger(it->getMusicSinger());
            item->setMusicAlbum(it->getMusicAlbumn());
            item->setMusicLike(it->getIsLike());
            QListWidgetItem*listItem = new QListWidgetItem(ui->pageMusicList);
            listItem->setSizeHint(QSize(ui->pageMusicList->width(),45));//设置item的初识状态；
            ui->pageMusicList->setItemWidget(listItem,item);
            connect(item,&ListItem::setIsLike,this,[=](bool isLike){
                it->setIsLike(isLike);//设置music中是否喜欢；
                emit updateLikePage(it->getId());//发送信号通知QQmusic更新页面；
            });
        }
    }
}

void CommonPage::addMusicToPlaylist(MusicList &musicList, QMediaPlaylist *playList)
{
    //每一个页面多有自己的_listId中放置了对应页面中的音乐；
    for(auto musicId:_listId)
    {
        auto it = musicList.findId(musicId);
        if(it != musicList.end())
        {
            playList->addMedia(it->getMusicUrl());//设置到列表中；
            qDebug()<<it->getMusicUrl();
        }
    }
}

QString CommonPage::getMusicId(int index)
{
    if(index>=_listId.size())
    {
        qDebug()<<"没有这首音乐";
        return "";
    }
    return _listId[index];
}

void CommonPage::setCurrentImage(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}

void CommonPage::on_playAllBt_clicked()
{
    //点击全部播放，会发射一个信号给miniQQmusic；
    emit playAll(_pageType);
}
